import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

const routes = [{
  path: '/',
  name: 'login',
  meta: {
    title: '登录页',
  },
  component: () => import(/* webpackChunkName: "incentiveplan" */ '../views/login/index.vue'),
},
{
  path: '/home/dataView',
  name: 'home',
  meta: {
    title: '首页',
  },
  component: () => import(/* webpackChunkName: "incentiveplandetail" */ '../views/home/index.vue'),
  children: [
    {
      path: '/home/dataView',
      name: 'dataView',
      meta: {
        title: '首页/数据展示',
      },
      component: () => import(/* webpackChunkName: "incentiveplandetail" */ '../views/dataView/dataView.vue'),
    },
    {
      path: '/home/chartView',
      name: 'chartView',
      meta: {
        title: '首页/图例展示',
      },
      component: () => import(/* webpackChunkName: "incentiveplandetail" */ '../views/chartView/chartView.vue'),
    },
  ],
},

];

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  // eslint-disable-next-line no-console
  console.log(to, from, next, '进入之前');
  document.title = to.meta.title;

  // token时效
  let currDate = new Date().getTime();
  let exceedTime = localStorage.getItem('exceedTime');
  if (currDate > exceedTime) {
    localStorage.removeItem('token', 'token');
  }

  // 跳转拦截
  if (to.path === '/') {
    next();
  } else {
    let token = localStorage.getItem('token');

    if (token === null || token === '') {
      next('/');
    } else {
      next();
    }
  }
});

export default router;
